<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['idUser']))
	exit();

function searchABMUsers()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('users.idUser, users.userName, users.userCompleteName, users.userEMail, users.expirationDate')
			->from('users')
			->join('typeuser','users.idTypeUser = typeuser.idTypeUser')
			->select('description')
			->edit_column('description','$1','getDescriptionRes(description)')
			->edit_column('users.expirationDate','$1','formatoFecha(users.expirationDate)')
			->select('users.userState')
			->edit_column('users.userState', '$1', 'getImageUserState(users.userState, users.idUser)')
			->where("users.userState <> '".$stateDeleted."'");

	return $datatables->generate();	
}

function getImageUserState($state, $idUser) 
{
	require("includes/constants.php");

	if($state == $stateActive)
		return '<center><img src="images/active.png" id="'.$idUser.'" /></center>';
	else
		return '<center><img src="images/inactive.png" id="'.$idUser.'" /></center>';
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	$mcrypt = new MCrypt();
	
	$expirationDate = date("Y-m-d", strtotime(str_replace("/", "-", $_REQUEST['userExpirationDate'])));

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
		
	if(!isset($_REQUEST["idUser"])){
		$strSQL = "SELECT userName FROM users WHERE userName = '".$_REQUEST["userName"]."' AND userState <> '$stateDeleted'";
		$rsUsers = $DB->Execute($strSQL);		

		if(!$rsUsers->EOF)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->msg = $a_languages['Common.ErrorDuplicado'];
			return json_encode($arrJSON);
		}
			
		$strSQL = "INSERT INTO users ( ";
		$strSQL .= "	userName, ";
		$strSQL .= "	userPassword, ";
		$strSQL .= "	userCompleteName, ";
		$strSQL .= "	userEMail, ";
		$strSQL .= "	userPhone, ";
		$strSQL .= "	userCellPhone, ";
		$strSQL .= "	userAddress, ";
		$strSQL .= "	userState, ";
		$strSQL .= "	expirationDate, ";
		$strSQL .= "	idTypeUser ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["userName"], "UTF-8")."', ";
		$strSQL .= "	'".$mcrypt->encrypt($_REQUEST["userPassword"])."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["userCompleteName"], "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["userEMail"]."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["userPhone"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["userCellPhone"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["userAddress"], "UTF-8")."', ";
		$strSQL .= "	'".$stateActive."', ";
		$strSQL .= "	'".$expirationDate."', ";
		$strSQL .= "	".$_REQUEST["cmbTypeUser"];
		$strSQL .= "	)";
		
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE users SET ";
		$strSQL .= "	userName = '".mb_strtoupper($_REQUEST["userName"], "UTF-8")."', ";
		if(!empty($_REQUEST["userPassword"]))
			$strSQL .= "	userPassword = '".$mcrypt->encrypt($_REQUEST["userPassword"])."', ";
		$strSQL .= "	userCompleteName = '".mb_strtoupper($_REQUEST["userCompleteName"], "UTF-8")."', ";
		$strSQL .= "	userEMail = '".$_REQUEST["userEMail"]."', ";
		$strSQL .= "	userPhone = '".mb_strtoupper($_REQUEST["userPhone"], "UTF-8")."', ";
		$strSQL .= "	userCellPhone = '".mb_strtoupper($_REQUEST["userCellPhone"], "UTF-8")."', ";
		$strSQL .= "	userAddress = '".mb_strtoupper($_REQUEST["userAddress"], "UTF-8")."', ";
		$strSQL .= "	userState = '".$stateActive."',";
		$strSQL .= "	expirationDate = '".$expirationDate."',";
		$strSQL .= "	idTypeUser = ".$_REQUEST["cmbTypeUser"];
		$strSQL .= " WHERE idUser = ". $_REQUEST["idUser"];
		
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE users SET userState='$stateDeleted' where idUser=" . $_REQUEST["idUser"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkEliminar'];
	}	
	return json_encode($arrJSON);
}

function editUser()
{
	require("includes/constants.php");
	
	$mcrypt = new MCrypt();
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsUsers = $DB->Execute("SELECT * FROM users WHERE userState <> '$stateDeleted' AND idUser=" . $_REQUEST["idUser"]); // execute query
						
	if(!$rsUsers->EOF){
		$joUser->data['userName'] = $rsUsers->fields("userName");
		$joUser->data['userCompleteName'] = $rsUsers->fields("userCompleteName");
		$joUser->data['userPassword'] = $mcrypt->decrypt($rsUsers->fields("userPassword"));
		$joUser->data['userEMail'] = $rsUsers->fields("userEMail");
		$joUser->data['userPhone'] = $rsUsers->fields("userPhone");
		$joUser->data['userAddress'] = $rsUsers->fields("userAddress");
		$joUser->data['userExpirationDate'] = date("d/m/Y", strtotime($rsUsers->fields("expirationDate")));
		$joUser->data['idTypeUser'] = $rsUsers->fields("idTypeUser");
		
		return json_encode($joUser->data);
	}
	else
		return json_encode('');
}

function getTypeUsers()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$rsTypeUsers = $DB->Execute("SELECT * FROM typeuser");

	$i=0;
	while(!$rsTypeUsers->EOF){
		$arrTypeUsers->data[$i]['idTypeUser'] = $rsTypeUsers->fields(0);
		$arrTypeUsers->data[$i++]['description'] = $a_languages[$rsTypeUsers->fields(1)];
		$rsTypeUsers->MoveNext();
	}

	return json_encode($arrTypeUsers->data);
}

function changeState()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$rsUsers = $DB->Execute("SELECT * FROM users WHERE idUser=" . $_REQUEST["idUser"]);

	if($rsUsers->fields('userState') == $stateActive) {
		$DB->Execute("UPDATE users SET userState = '$stateInactive' WHERE idUser = " . $_REQUEST["idUser"]);
		$arrJSON->status = "OK";
		$arrJSON->userState = $stateInactive;
	}
	else {
		$DB->Execute("UPDATE users SET userState = '$stateActive' WHERE idUser = " . $_REQUEST["idUser"]);
		$arrJSON->status = "OK";
		$arrJSON->userState = $stateActive;
	}

	return json_encode($arrJSON);
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editUser();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMUsers();
		break;
	case "getTypeUsers":
		echo getTypeUsers();
		break;
	case "changeState":
		echo changeState();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;		
}
?>
